// 结构体排序
#include <stdio.h>

struct stu
{
    int num;
    char name[20];
    int count;
};

int sortcount(struct stu s[], int n)
{
    int i, j, sum = 0;
    struct stu t;
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n - 1 - i; j++)
            if (s[j].count < s[j + 1].count)
            {
                t = s[j];
                s[j] = s[j + 1];
                s[j + 1] = t;
            }
    }
    for (i = 0; i < 5; i++)
        sum = sum + s[i].count;
    return sum;
}

void main()
{
    struct stu s[5] = {{11, "li", 1}, {22, "zhang", 3}, {33, "wang", 1}, {44, "zhao", 2}, {55, "liang", 4}};

    int i, sum;
    FILE *fp;
    fp = fopen("myf3.txt", "w");

    sum = sortcount(s, 5);
    for (i = 0; i < 5; i++)
    {
        printf("%d\t%s\t%d\n", s[i].num, s[i].name, s[i].count);
        fprintf(fp, "%d\t%s\t%d\n", s[i].num, s[i].name, s[i].count);
    }
    printf("sum=%d\n", sum);
    fprintf(fp, "sum=%d\n", sum);
    fprintf(fp, "\nMy exam number is: 208140133");
    fclose(fp);
}
